//
//  LRMoveLineBackground.m
//  lineRunner_cocos2d
//
//  Created by chen jingsen on 13-1-18.
//  Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import "LRMoveLineBackground.h"


@implementation LRMoveLineBackground

- (id)init
{
    self = [super init];
    if (self) {
        [self initData];
        [self initStaticBox];
//        [self initBox];
        [self initFloorBatchNode];
        [self scheduleUpdate];
    }
    return self;
}

- (void)dealloc{
    [produceLogic release];
    [super dealloc];
}

- (void)initData
{
    moveRate = 100;
    produceLogic = [[LRBoxProduceLogic alloc] init];
}

- (void)initStaticBox
{
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    
    CCTexture2D* gameArtTexture = [[CCTextureCache sharedTextureCache] addImage:@"gElem.png"];
    _boxBatchNode = [CCSpriteBatchNode batchNodeWithTexture:gameArtTexture];
    [self addChild:_boxBatchNode];
    
    CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    [frameCache addSpriteFramesWithFile:@"gElem.plist"];
    
    CCSpriteFrame *frame = [frameCache spriteFrameByName:@"blackbox.png"];
    CCSprite *sprit = [CCSprite spriteWithSpriteFrame:frame];
    boxCount = screenSize.width / sprit.textureRect.size.width*1.3;
    for (int i = 0; i < boxCount; i++) {
        CCSpriteFrame *frame = [frameCache spriteFrameByName:@"blackbox.png"];
        CCSprite *sprit = [CCSprite spriteWithSpriteFrame:frame];
        sprit.anchorPoint  = CGPointMake(0.0f, 0.5f);
        sprit.position = CGPointMake(sprit.textureRect.size.width*i - (i+1),sprit.textureRect.size.height*0.5);
//        parallaxWith += sprit.textureRect.size.width + 1;
        
//        [_boxBatchNode addChild:sprit];
        [self addChild:sprit];
    }

}

- (void)initBox
{
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    
    CCTexture2D* gameArtTexture = [[CCTextureCache sharedTextureCache] addImage:@"gElem.png"];
    _boxBatchNode = [CCSpriteBatchNode batchNodeWithTexture:gameArtTexture];
    [self addChild:_boxBatchNode];
    
    CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    [frameCache addSpriteFramesWithFile:@"gElem.plist"];
    
    CCSpriteFrame *frame = [frameCache spriteFrameByName:@"blackbox.png"];
    CCSprite *sprit = [CCSprite spriteWithSpriteFrame:frame];
    boxCount = (screenSize.width / sprit.textureRect.size.width)*2;
    for (int i = 0; i < boxCount; i++) {
        CCSpriteFrame *frame = [frameCache spriteFrameByName:@"blackbox.png"];
        CCSprite *sprit = [CCSprite spriteWithSpriteFrame:frame];
        sprit.anchorPoint  = CGPointMake(0.0f, 0.5f);
        
        sprit.position = CGPointMake(sprit.textureRect.size.width*i - (i+1),sprit.textureRect.size.height*0.5);
        parallaxWith += sprit.textureRect.size.width + 1;

        [_boxBatchNode addChild:sprit];
    }
}

- (void)initFloorBatchNode
{
    
    CCTexture2D* gameArtTexture = [[CCTextureCache sharedTextureCache] addImage:@"gElem.png"];
    _floorBatchNode = [CCSpriteBatchNode batchNodeWithTexture:gameArtTexture];
    [self addChild:_floorBatchNode];
    
    CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    [frameCache addSpriteFramesWithFile:@"gElem.plist"];
    
    float lineboxWith;
    for (int i = 0; i < 20; i++) {
        CCSpriteFrame *frame = [frameCache spriteFrameByName:@"blackbox.png"];
        CCSprite *sprit = [CCSprite spriteWithSpriteFrame:frame];
        sprit.anchorPoint  = CGPointMake(0.0f, 0.5f);
        float boxSpritX ;
        float boxSpritY ;
        BoxLocation location = [produceLogic boxUpOrDownlocation];
        if (location == BoxLocationUp) {
            boxSpritY = sprit.textureRect.size.height*4;
        }else{
            boxSpritY = sprit.textureRect.size.height*1.5;
        }
        
        boxSpritX = [produceLogic boxDistance];
        
        sprit.position = CGPointMake(lineboxWith + boxSpritX,boxSpritY);
        lineboxWith += boxSpritX;
        NSLog(@"sprit.position: %@",NSStringFromCGPoint(sprit.position));
        [_floorBatchNode addChild:sprit];
    }
    
}

- (void)update:(ccTime)delta
{
//    CCSprite *sprit;
//    CCARRAY_FOREACH([_boxBatchNode children], sprit)
//    {
//        CGPoint spritPoint = sprit.position;
//        spritPoint.x -= delta*moveRate;
//        
//        if (spritPoint.x < -sprit.textureRect.size.width) {
//            spritPoint.x += parallaxWith - boxCount*2;
//        }
//        sprit.position = spritPoint;
//    }
    
    CCSprite *floorSprite;
    float pointX = -1;
    CCARRAY_FOREACH([_floorBatchNode children], floorSprite)
    {
        CGPoint spritPoint = floorSprite.position;
        spritPoint.x -= delta*moveRate;
        if (spritPoint.x < -floorSprite.textureRect.size.width) {
            float boxSpritX ;
            float boxSpritY ;
            BoxLocation location = [produceLogic boxUpOrDownlocation];
            if (location == BoxLocationUp) {
                boxSpritY = floorSprite.textureRect.size.height*4;
            }else{
                boxSpritY = floorSprite.textureRect.size.height*1.5;
            }
            
            boxSpritX = [produceLogic boxDistance];
            if (pointX == -1) {
                CCSprite *spritx = [[_floorBatchNode children] lastObject];
                spritPoint.x += spritx.position.x + boxSpritX;//pointX + boxSpritX
            }

            
            spritPoint.y = boxSpritY;
            pointX = spritPoint.x;

        }
        floorSprite.position = spritPoint;
        NSLog(@"spritPoint:%@",NSStringFromCGPoint(spritPoint));
    }
}

@end
